﻿using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using MyMeetup.Entities;

namespace MyMeetup.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MemberPageViewModel : ViewModelBase
    {
        private DataService _dataService;
        
        public RelayCommand LoadedCommand { get; private set; }
        public string MemberId { get; set; }
        public string GroupId { get; set; } 
    

        /// <summary>
        /// Initializes a new instance of the MemberPageViewModel class.
        /// </summary>
        public MemberPageViewModel()
        {
            _dataService = new DataService();

            LoadedCommand = new RelayCommand(PageLoaded);

            Messenger.Default.Register<PropertyChangedMessage<IProfile>>(this,
                                                             (action) => UpdateMember(action.NewValue));

            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
        }

        private void PageLoaded()
        {
            //_dataService.GetMemberProfile(int.Parse(MemberId), int.Parse(GroupId), (x) => CurrentProfile = x);
        }


        private void UpdateMember(IProfile memberInfo)
        {
            CurrentProfile = memberInfo;
        }


        /// <summary>
        /// The <see cref="CurrentProfile" /> property's name.
        /// </summary>
        public const string CurrentProfilePropertyName = "CurrentProfile";

        private IProfile _currentProfile;

        /// <summary>
        /// Gets the CurrentProfile property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public IProfile CurrentProfile
        {
            get
            {
                return _currentProfile;
            }

            set
            {
                if (_currentProfile == value)
                {
                    return;
                }

                var oldValue = _currentProfile;
                _currentProfile = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(CurrentProfilePropertyName);
            }
        }
       
    }
}